import $store from '@/store/index.js';
export default {
	/* 获取好友列表 */
	getFrindList() {
		return new Promise((resolve, reject) => {
			// 然后执行join操作来联查friends与userinfo表
			$store.state.user.sqlite.friends
				.fields('friends.friend_idx, friends.create_time,userinfo.friend_remark,userinfo.nick_name, userinfo.avatar_url')
				.join('userinfo', 'friend_idx', 'user_id')
				.get() // 执行查询
				.then(results => {
					resolve(results)
				})
				.catch(error => {
					reject(error)
				});
		})
	},
	//批量插替好友到列表
	batchReplaceFrindList(data){
		return $store.state.user.sqlite.friends.batchReplace(data)
	},
	//批量插如修改好友到列表
	batchAddOrEditFrindList(data,key){
		return $store.state.user.sqlite.friends.batchAddOrEdit(data,key)
	},
	//更新或添加好友列表
	async addOrEditFrindList(data) {
		//逻辑是先插入如果插入失败，就更新
		try{
			await $store.state.user.sqlite.friends.create(data)
			return true
		}catch(e){
			try{
				await this.editFrindList(data.friend_idx,data)
				return true
			}catch(e){
				console.log(e);
				throw e
			}
		}
	},
	/* 修改好友列表 */
	editFrindList(friend_idx,data){
		return new Promise(async(resolve,reject)=>{
			try{
				let editres = await $store.state.user.sqlite.friends.where({ field: 'friend_idx', val: `'${friend_idx}'`}).update(data)
				resolve(editres)
			}catch(e){
				reject(e)
			}
		})
	},
	/* 删除分组更新好友的分组 */
	editFrindListGroup(friend_group){
		return new Promise(async(resolve,reject)=>{
			try{
				let editres = await $store.state.user.sqlite.friends.where({ field: 'friend_group', val: `'${friend_group}'`}).update({
					friend_group:0
				})
				resolve(editres)
			}catch(e){
				reject(e)
			}
		})
	},
	/* 删除好友列表 */
	delFrindList(friend_idx){
		return new Promise(async(resolve,reject)=>{
			try{
				let deleteres = await $store.state.user.sqlite.friends.where({ field: 'friend_idx', val: `'${friend_idx}'`}).delete()
				resolve(deleteres)
			}catch(e){
				reject(e)
			}
		})
	},
	
	
	
	
	//获取用户信息
	getUserInfo(user_id,fields) {
		return new Promise((resolve, reject) => {
			$store.state.user.sqlite.userinfo.where({ field: 'user_id', val: `'${user_id}'` }).fields(fields).first()
				.then(results => {
					resolve(results)
				})
				.catch(error => {
					reject(error)
				});
		})
	},
	//更新或添加用户信息,轻易不要调用，因为会出现一个问题，如果新的data值不全会把旧的已存在的值也给替换掉
	replaceUserInfo(data) {
		return new Promise((resolve, reject) => {
			$store.state.user.sqlite.userinfo.replace(data)
				.then(results => {
					console.log('更新或添加用户信息',results);
					resolve(results)
				})
				.catch(error => {
					console.log('更新或添加用户信息',error);
					reject(error)
				});
		})
	},
	//批量添加或修改用户信息
	async batchAddOrEditUserInfo(data,key) {
		return $store.state.user.sqlite.userinfo.batchAddOrEdit(data,key)
	},
	
	//更新或添加用户信息
	async addOrEditUserInfo(data) {
		//逻辑是先插入如果插入失败，就更新
		try{
			await $store.state.user.sqlite.userinfo.create(data)
			//更新内存中的存储
			$store.commit('setUser', { userId:data.user_id, userInfo: data });
			return true
		}catch(e){
			try{
				await this.editUserInfo(data.user_id,data)
				return true
			}catch(e){
				console.log(e);
				throw e
			}
		}
	},
	//更新用户信息
	editUserInfo(user_id,data) {
		//主动修改用户的话,一般调用这个方法都是数据不全,如果数据全直接调用addOrEditUserInfo这个方法了，更改存储的用户信息
		$store.commit('editUser', { userId:user_id, userInfo: data });
		return $store.state.user.sqlite.userinfo.where({ field: 'user_id', val: `'${user_id}'`}).update(data)
	},
	//自定义修改
	editUserInfoCustom(where,data) {
		return $store.state.user.sqlite.userinfo.where(where).update(data)
	}
}